package net.sky.oa.model;

import java.util.Set;
import java.util.List;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Temporal;
import javax.persistence.FetchType;
import javax.persistence.TemporalType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

/**
 * 工作总结表
 * OffTask. @author hond
 */
@Entity
@Table(name = "OFF_TASK")//工作任务表
public class OffTask implements java.io.Serializable {

	private static final long serialVersionUID = 5673991033245515351L;
	
	// Fields
    private Long id;
	private String title;   //任务标题
	private String content; //任务内容
	private String annexurl;//附件地址
	private Integer type;      //任务类型
	private Integer rank;   //紧要程度：1重要2一般
	private Double rate;    //完成率
	private Date begintime;
	private Date endtime;
	private Date lasttime;  //最后修改时间
	private Integer status;    //状态
	private SysEmployee user; //用户id
	
	private Set<FileAttach> taskFiles;
	
	private Set<OffTaskUser> offTaskUsers;

	/** default constructor */
	public OffTask() {
	}

	@Id
	@Column(name = "ID", unique = true, nullable = false)
	@SequenceGenerator(name="SQ_OFFTASK_ID",sequenceName="SQ_OFFTASK_ID",allocationSize=1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="SQ_OFFTASK_ID")
	public Long getId() {
	    return id;
	}

    public void setId(Long id){
	    this.id = id;
	}
	
	@Column(name = "TITLE", length = 60)
	public String getTitle() {
		return this.title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Column(name = "CONTENT")
	public String getContent() {
		return this.content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	@Column(name = "ANNEXURL", length = 100)
	public String getAnnexurl() {
		return this.annexurl;
	}

	public void setAnnexurl(String annexurl) {
		this.annexurl = annexurl;
	}

	@Column(name = "TYPE", precision = 2, scale = 0)
	public Integer getType() {
		return this.type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	@Column(name = "RANK", precision = 1, scale = 0)
	public Integer getRank() {
		return this.rank;
	}

	public void setRank(Integer rank) {
		this.rank = rank;
	}

	@Column(name = "RATE", precision = 4, scale = 3)
	public Double getRate() {
		return this.rate;
	}

	public void setRate(Double rate) {
		this.rate = rate;
	}
	
	@Temporal(TemporalType.DATE)
	@Column(name = "BEGINTIME")
	public Date getBegintime() {
		return begintime;
	}

	public void setBegintime(Date begintime) {
		this.begintime = begintime;
	}
	
	@Temporal(TemporalType.DATE)
	@Column(name = "ENDTIME")
	public Date getEndtime() {
		return endtime;
	}

	public void setEndtime(Date endtime) {
		this.endtime = endtime;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "LASTTIME", length = 7)
	public Date getLasttime() {
		return this.lasttime;
	}

	public void setLasttime(Date lasttime) {
		this.lasttime = lasttime;
	}

	@Column(name = "STATUS", precision = 2, scale = 0)
	public Integer getStatus() {
		return this.status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "USER_ID")
	public SysEmployee getUser() {
		return this.user;
	}

	public void setUser(SysEmployee user) {
		this.user = user;
	}
	@ManyToMany(fetch=FetchType.EAGER)
	@JoinTable(name="task_file",joinColumns={@JoinColumn(name="task_id")},inverseJoinColumns={@JoinColumn(name="file_id")})  
	@Cascade(value={CascadeType.PERSIST,CascadeType.MERGE}) 
	public Set<FileAttach> getTaskFiles() {
		return taskFiles;
	}

	public void setTaskFiles(Set<FileAttach> taskFiles) {
		this.taskFiles = taskFiles;
	}
	@OneToMany(fetch=FetchType.EAGER,mappedBy="task")
	@Cascade(value={CascadeType.SAVE_UPDATE,CascadeType.DELETE_ORPHAN}) 
	public Set<OffTaskUser> getOffTaskUser() {
		return offTaskUsers;
	}

	public void setOffTaskUser(Set<OffTaskUser> offTaskUsers) {
		this.offTaskUsers = offTaskUsers;
	}
	
	/**
	 * 功能 ：
	 * 1、把原来选中现在选中的保留
	 * 2、把原来选中现在没有选中的删除掉
	 * 3、把原来没有选中现在选中的增加进去
	 * @param checkedIds
	 */
	public void mergeByCheckedIds(final Collection<Long> checkedIds) {
		Set<Long> set=new HashSet<Long>();
		List<Long> list=new ArrayList<Long>();
		for(Iterator<Long> iter=checkedIds.iterator();iter.hasNext();){
			Long id=iter.next();
			if(set.add(id)){
				list.add(id);
			}
		}
		checkedIds.clear();
		checkedIds.addAll(list);//去除checkedIds中的重复记录
		
		Set<OffTaskUser> taskUsers=this.getOffTaskUser();
		if(null==taskUsers){
			taskUsers=new HashSet<OffTaskUser>();
		}
		if (checkedIds == null){
			taskUsers.clear();
			return;
		}
		Iterator<OffTaskUser> iter=taskUsers.iterator();
		
		try {
			while(iter.hasNext())
			{
				OffTaskUser e=iter.next();
				Long id=e.getRusr().getId();
				if(!checkedIds.contains(id))
				{
					iter.remove();
				}else
				{
					checkedIds.remove(id);
				}
			}
			
			for(Long id:checkedIds){
				OffTaskUser offTaskUser=new OffTaskUser();
				SysEmployee user=new SysEmployee();
				user.setId(id);
				offTaskUser.setRusr(user);
				offTaskUser.setTask(this);
				offTaskUser.setIsrecieve(1);
				offTaskUser.setRate(0.0);
				offTaskUser.setSendtime(new Date());
				
				taskUsers.add(offTaskUser);
			}
			this.setOffTaskUser(taskUsers);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
}